package com.qmhl.toolposition.adapter

import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseMultiItemAdapter
import com.qmhl.toolposition.api.content.Goods
import com.qmhl.toolposition.databinding.ItemRvVipSanyueBinding
import com.qmhl.toolposition.databinding.ItemRvVipYinianBinding
import com.qmhl.toolposition.databinding.ItemRvVipYongjiuBinding

class VipShowAdapter(data: List<Goods>) :
    BaseMultiItemAdapter<Goods>(data) {


    class YongjiuVH(val viewBinding: ItemRvVipYongjiuBinding) :
        RecyclerView.ViewHolder(viewBinding.root)


    class YinianVH(val viewBinding: ItemRvVipYinianBinding) :
        RecyclerView.ViewHolder(viewBinding.root)

    class SanyueVH(val viewBinding: ItemRvVipSanyueBinding) :
        RecyclerView.ViewHolder(viewBinding.root)

    init {
        addItemType(
            VipShowAdapter.ITEM_YONGJIU,
            object : OnMultiItemAdapterListener<Goods, VipShowAdapter.YongjiuVH> {
                override fun onCreate(
                    context: Context,
                    parent: ViewGroup,
                    viewType: Int
                ): VipShowAdapter.YongjiuVH {
                    // 创建 viewholder
                    val viewBinding =
                        ItemRvVipYongjiuBinding.inflate(
                            LayoutInflater.from(context),
                            parent,
                            false
                        )
                    return VipShowAdapter.YongjiuVH(viewBinding)
                }

                override fun onBind(
                    holder: VipShowAdapter.YongjiuVH,
                    position: Int,
                    item: Goods?
                ) {
                    // 绑定 item 数据
                    if (item!!.isSelect) {
                        holder.viewBinding.tvTime.setTextColor(Color.RED)
                        holder.viewBinding.tvTime.textSize = 20f
                    } else {
                        holder.viewBinding.tvTime.setTextColor(Color.BLACK)
                        holder.viewBinding.tvTime.textSize = 16f

                    }
                    holder.viewBinding.tvRealPrice.text = item?.realPrice.toString()
                    holder.viewBinding.tvShowPrice.text = item?.showPrice.toString()
                    holder.viewBinding.tvShowPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG

                }
            })


            .addItemType(
                VipShowAdapter.ITEM_YINIAN,
                object : OnMultiItemAdapterListener<Goods, VipShowAdapter.YinianVH> { //
                    override fun onCreate(
                        context: Context,
                        parent: ViewGroup,
                        viewType: Int
                    ): VipShowAdapter.YinianVH {
                        // 创建 viewholder
                        val viewBinding =
                            ItemRvVipYinianBinding.inflate(
                                LayoutInflater.from(context),
                                parent,
                                false
                            )
                        return VipShowAdapter.YinianVH(viewBinding)
                    }

                    override fun onBind(
                        holder: VipShowAdapter.YinianVH,
                        position: Int,
                        item: Goods?
                    ) {
                        if (item!!.isSelect) {
                            holder.viewBinding.tvTime.setTextColor(Color.RED)
                            holder.viewBinding.tvTime.textSize = 20f
                        } else {
                            holder.viewBinding.tvTime.setTextColor(Color.BLACK)
                            holder.viewBinding.tvTime.textSize = 16f
                        }

                        holder.viewBinding.tvRealPrice.text = item?.realPrice.toString()
                        holder.viewBinding.tvShowPrice.text = item?.showPrice.toString()
                        holder.viewBinding.tvShowPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                    }

                    override fun isFullSpanItem(itemType: Int): Boolean {
                        // 使用GridLayoutManager时，此类型的 item 是否是满跨度
                        return true;
                    }

                })

            .addItemType(
                VipShowAdapter.ITEM_SANYUE,
                object : OnMultiItemAdapterListener<Goods, VipShowAdapter.SanyueVH> { // 类型  已同意发起方
                    override fun onCreate(
                        context: Context,
                        parent: ViewGroup,
                        viewType: Int
                    ): VipShowAdapter.SanyueVH {
                        // 创建 viewholder
                        val viewBinding =
                            ItemRvVipSanyueBinding.inflate(
                                LayoutInflater.from(context),
                                parent,
                                false
                            )
                        return VipShowAdapter.SanyueVH(viewBinding)
                    }

                    override fun onBind(
                        holder: VipShowAdapter.SanyueVH,
                        position: Int,
                        item: Goods?
                    ) {
                        if (item!!.isSelect) {
                            holder.viewBinding.tvTime.textSize = 20f
                            holder.viewBinding.tvTime.setTextColor(Color.RED)
                        } else {
                            holder.viewBinding.tvTime.textSize = 16f
                            holder.viewBinding.tvTime.setTextColor(Color.BLACK)
                        }
                        holder.viewBinding.tvRealPrice.text = item?.realPrice.toString()
                        holder.viewBinding.tvShowPrice.text = item?.showPrice.toString()
                        holder.viewBinding.tvShowPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG

                    }

                    override fun isFullSpanItem(itemType: Int): Boolean {
                        // 使用GridLayoutManager时，此类型的 item 是否是满跨度
                        return true;
                    }

                })


            .onItemViewType { position, list -> // 根据数据，返回对应的 ItemViewType
                val data = list[position]
                if (data.id == 1) {
                    VipShowAdapter.ITEM_YONGJIU
                } else if (data.id == 2) {
                    VipShowAdapter.ITEM_YINIAN
                } else if (data.id == 3) {
                    VipShowAdapter.ITEM_SANYUE
                } else {
                    VipShowAdapter.ITEM_YONGJIU
                }
            }
    }


    companion object {
        private const val ITEM_YONGJIU = 1
        private const val ITEM_YINIAN = 2
        private const val ITEM_SANYUE = 3


    }


}